home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / ntfs / user / ntcat.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-15  |  4.3 KB  |  205 lines

  1. /*
  2.  *  ntcat.c
  3.  *
  4.  *  Copyright (C) 1995-1997 Martin von L÷wis
  5.  *  Copyright (C) 1997 RΘgis Duchesne
  6.  */
  7.  
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #ifdef HAVE_GETOPT_H
  15. #include <getopt.h>
  16. #else
  17. #define getopt_long(a,v,o,ol,x)        getopt(a,v,o)
  18. #endif
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #ifdef _WINDOWS
  23. #include <fcntl.h>
  24. #endif
  25. #ifdef HAVE_IO_H
  26. #include <io.h>
  27. #endif
  28. #include <string.h>
  29. #include "ntfstypes.h"
  30. #include "struct.h"
  31. #include "util.h"
  32. #include "inode.h"
  33. #include "nttools.h"
  34.  
  35. char *short_opts="o:s:B:b:1Vh";
  36. #ifdef HAVE_GETOPT_H
  37. struct option options[]={
  38.     {"offset",1,0,'o'},
  39.     {"size",1,0,'s'},
  40.     {"bias",1,0,'B'},
  41.     {"buflen",1,0,'b'},
  42.     {"8859-1",0,0,'1'},
  43.     {"version",0,0,'V'},
  44.     {"help",0,0,'h'},
  45.     {0,0,0,0}
  46. };
  47. #endif
  48.  
  49. char usage_str[]=
  50. "ntcat: dumps the contents of an NTFS file to stdout\n"
  51. "ntcat [OPTIONS] [directory/]filename\n"
  52. "  --offset, -o offset         Use offset\n"
  53. "  --size, -s size           Only first size bytes\n"
  54. "  --bias, -B bias           Use partition bias\n"
  55. "  --buflen, -b buflen       Read with buffer size buflen\n"
  56. "  --8859-1, -1              Use charset ISO-8859-1\n"
  57. "                            (default is UTF-8)\n"
  58. "  --version, -V             Display version\n"
  59. "  --help, -h                Display this message\n"
  60. ;
  61.  
  62.  
  63. void usage(void)
  64. {
  65. /*      fprintf(stderr,"ntcat [-f device] [-o offset] [-l size] [-1] [directory/]filename\n");*/
  66.     fprintf(stderr,usage_str);
  67.  
  68. }
  69.  
  70. /* print the file ino on stdout */
  71. void ntfs_cat_file(ntfs_inode *ino,int offset,int length,int bufsize)
  72. {
  73.     int position;
  74.     char *buf;
  75.     ntfs_io io;
  76.  
  77.     position=offset;
  78.     if(!bufsize) bufsize=8192;
  79.     buf=(char*)malloc(bufsize);
  80.     /* Very large file */
  81.     if(!buf)
  82.     {
  83.         bufsize=8192;
  84.         buf=(char*)malloc(bufsize);
  85.         if(!buf)
  86.         {
  87.             perror("ntcat");
  88.             exit(0);
  89.         }
  90.     }
  91.     io.fn_put=ntfs_put;
  92.     io.fn_get=0;
  93.     io.do_read=1;
  94.     io.param=buf;
  95.     io.size=bufsize;
  96.     /* read from the unnamed data attribute */
  97.     while(ntfs_read_attr(ino,ino->vol->at_data,
  98.                  NULL,position,&io)==0 && io.size)
  99.     {
  100.         if(write(1,buf,io.size)!=io.size){
  101.             perror("write");
  102.             exit(1);
  103.         }
  104.         position+=io.size;
  105.         length-=io.size;
  106.         if(length==0)
  107.             break;
  108.         io.param=buf;
  109.         io.size=bufsize;
  110.     }
  111.     free(buf);
  112. }
  113.  
  114. int main(int argc,char *argv[])
  115. {
  116.     int c;
  117.     char device[256]={'\0'};        /* KM */
  118.     char *it;                       /* KM */
  119. #ifdef _WINDOWS
  120.     devname[32];
  121. #endif
  122.     int offset=0;
  123.     int length=0;
  124.     char *name;
  125.     ntfs_volume *volume;
  126.     ntfs_inode ino;
  127.     int inum;
  128.     int bias=0;
  129.     int buflen=8192;
  130.     extern int opterr,optind;
  131.     extern char* optarg;
  132.     unsigned int charset=nct_utf8;
  133.  
  134.     opterr=1;
  135.     while((c=getopt_long(argc,argv,short_opts,options,NULL))>0)
  136.         switch(c)
  137.         {
  138.         case 'o': offset=strtol(optarg,NULL,0);break;
  139.         case 's': length=strtol(optarg,NULL,0);break;
  140.         case 'B': bias=strtol(optarg,NULL,0);break;
  141.         case 'b': buflen=strtol(optarg,NULL,0);break;
  142.         case '1': charset=nct_iso8859_1;break;
  143.         case 'V': printf("ntcat " NTFS_VERSION "\n");exit(0);break;
  144.         case 'h': usage();exit(0);break;
  145.         }
  146.     if(optind+1!=argc){
  147.         usage();
  148.         exit(1);
  149.     }
  150.     name=argv[optind];
  151.     for(it=name+2;*it && *it!='/';it++)
  152.         /*nothing*/;
  153.     if(it!=name+2)
  154.     {
  155.       strcpy(device,"/dev/");
  156.       strncpy(device+5,name+2,(it-name)-2);
  157.       device[(it-name)+3]='\0';
  158.     }
  159. #ifdef _WINDOWS
  160.     _setmode(fileno(stdout),_O_BINARY);
  161.     if(!device && name[1]=':'){
  162.         strcpy(devname,"\\\\.\\C:");
  163.         devname[4] = name[0];
  164.         name+=3;
  165.         device=devname;
  166.     }
  167. #endif
  168.     volume=ntfs_open_volume(device,bias,1,0);
  169.     if(!volume)return 1;
  170.     volume->nct=charset;
  171.     inum=5;
  172.     name=it;                /* KM */
  173.     if(*name=='/')name++;   /* KM */
  174.     /* walk the directory tree */
  175.     do{
  176.         char *next;
  177.         if(ntfs_init_inode(&ino,volume,inum)){
  178.             fprintf(stderr,"error finding %s\n",name);
  179.             return 1;
  180.         }
  181.         if(!name || !*name)break;
  182.         next=strpbrk(name,NTFS_PATH_SEP);
  183.         if(next){
  184.             *next='\0';
  185.             next++;
  186.         }
  187.         inum=ntfs_find_file(&ino,name);
  188.         if(inum==-1){
  189.             fprintf(stderr,"%s not found\n",name);
  190.             return 1;
  191.         }
  192.         name=next;
  193.     }while(1);
  194.     if(ntfs_init_inode(&ino,volume,inum))
  195.         fprintf(stderr,"error opening %s\n",name);
  196.     ntfs_cat_file(&ino,offset,length,buflen);
  197.     return 0;
  198. }
  199.  
  200. /*
  201.  * Local variables:
  202.  * c-file-style: "linux"
  203.  * End:
  204.  */
  205.